package com.syht.wechatpay.tools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * 访问远程服务器
 * 
 */
public class RemoteServer {

//	public static String sendPost(String server, String method, String
//			params) {
//
//		try {
//
//			URL url = new URL(server + method + "?"+params);
//			HttpURLConnection con = (HttpURLConnection) url.openConnection();
//			InputStreamReader in = new InputStreamReader(con.getInputStream(),
//					"utf-8");
//			BufferedReader bfreader = new BufferedReader(in);
//			StringBuffer sb = new StringBuffer();
//			String line = "";
//			while ((line = bfreader.readLine()) != null) {
//				sb.append(line);
//			}
//			System.out.println("结果DDD：" + sb.toString());
//			return sb.toString();
//
//		} catch (MalformedURLException e) {
//
//			e.printStackTrace();
//		} catch (IOException e) {
//
//			e.printStackTrace();
//		}
//
//		return "";
//	}

	public static String sendPost(String requestUrl, String params) {

		String strRet = "";
		try {

			URL url = new URL(requestUrl);
			URLConnection con = url.openConnection();
			con.setDoOutput(true);
			con.setRequestProperty("Pragma:", "no-cache");
			con.setRequestProperty("Cache-Control", "no-cache");
			con.setRequestProperty("Content-Type", "text/xml");
			OutputStreamWriter out = new OutputStreamWriter(
					con.getOutputStream());
			out.write(new String(params.getBytes("UTF-8")));
			out.flush();
			out.close();
			BufferedReader br = new BufferedReader(new InputStreamReader(
					con.getInputStream(), "UTF-8"));
			String line = "";
			for (line = br.readLine(); line != null; line = br.readLine()) {
				strRet += line;
			}
		} catch (MalformedURLException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}

		return strRet;
	}

	public static String sendGet(String requestUrl, String params) {

		String result = "";
		BufferedReader in = null;
		try {

			URL url = new URL(requestUrl + params);
			URLConnection conn = url.openConnection();
			conn.setRequestProperty("Pragma:", "no-cache");
			conn.setRequestProperty("Cache-Control", "no-cache");
			conn.setRequestProperty("Content-Type", "text/xml");
			conn.connect();
			in = new BufferedReader(new InputStreamReader(
					conn.getInputStream(), "GBK"));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {

			System.out.println("发送GET请求出现异常！" + e);
			e.printStackTrace();
		} finally {

			try {

				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {

				ex.printStackTrace();
			}
		}
		return result;

	}

}
